package com.inspinia.base.util;
/**
 * Created by WangJun on 2016/8/2
 */

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.net.URL;

/**
 * @类名: XmlParser
 * @类功能描述: 解析xml
 * @时间 ：2011-4-13 上午08:33:50
 * Created by WangJun on 2016/8/3 0003.
 */
public class ParseXmlUtil {
    /**
     * 对象转换成xml字符串
     *
     * @param obj
     * @return
     */
    public static String parseObject2Xml(Object obj) {
        String xml = "";
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = jaxbContext.createMarshaller();
            StringWriter writer = new StringWriter();
            marshaller.marshal(obj, writer);
            xml = writer.toString();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xml;
    }

    /**
     * xml字符串转成对象
     *
     * @param xml
     * @param object
     * @return
     */
    public static Object parseXml2Object(String xml, Object object) {
        Object obj = null;
        try {
            JAXBContext context = JAXBContext.newInstance(object.getClass());
            Unmarshaller unmarshaller = context.createUnmarshaller();
            obj = (Object) unmarshaller.unmarshal(new StringReader(xml));
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return obj;
    }

    /**
     * 读取本地的xml文件
     *
     * @return
     */
    public static Object LocalXmlFile2Object(Object object, String filePath) throws IOException {
        File file = new File(filePath);
        if (file.isFile() && file.exists()){
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file), "UTF-8"));
                StringBuffer xml = new StringBuffer();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    xml.append(line);
                }
                //读取文件 转换成Document
            object = parseXml2Object(xml.toString(), object);

            return object;
        }else {
            return null;
        }
    }


    /**
     * 读取指定URl的xml文件
     *
     * @return
     */
    public static Object XmlFile2Object(Object object, String filePath) {
        InputStream is = null;
        try {
            URL url = new URL(filePath);
            is = url.openStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"));
            StringBuffer xml = new StringBuffer();
            String line = null;
            while ((line = reader.readLine()) != null) {
                xml.append(line);
            }
            //读取文件 转换成Document
            if (!StringUtils.isBlank(xml.toString()))
                object = parseXml2Object(xml.toString(), object);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return object;
    }

    /**
     * xml字符串写入到指定文件地址
     *
     * @param object
     * @throws IOException
     */
    public static void String2XmlFile(Object object, String filePath) {
        String xmlStr = parseObject2Xml(object);
        SAXReader saxReader = new SAXReader();
        Document document;
        try {
            document = saxReader.read(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));
            OutputFormat format = OutputFormat.createPrettyPrint();
            /** 将document中的内容写入文件中 */
            XMLWriter writer = new XMLWriter(new FileWriter(new File(filePath)), format);
            writer.write(document);
            writer.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}